import Vue from 'vue'
import VueRouter from 'vue-router'
import Footer from '@/components/footer.vue'
import store from '@/store'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  // 首页
  {
    path: '/home',
    name: 'home',
    components: { default: () => import('@/views/home/index'), footer: Footer }
  },
  // 个人中心
  {
    path: '/myuser',
    name: 'myuser',
    components: { default: () => import('@/views/myuser/index'), footer: Footer }
  },
  // 全部订单
  {
    path: '/allorder/:active',
    name: 'allorder',
    component: () => import('@/views/allorder/index')
  },
  // 评价
  {
    path: '/taking',
    name: 'taking',
    component: () => import('@/views/taking/index')
  },
  // 退换/售后
  {
    path: '/Aftermarket',
    name: 'Aftermarket',
    component: () => import('@/views/Aftermarket/index')
  },
  // 编辑用户资料
  {
    path: '/edituser',
    name: 'edituser',
    component: () => import('@/views/myuser/edituser')
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/cart',
    name: 'cart',
    components: {
      default: () => import('@/views/cart/index'),
      footer: Footer
    }
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('@/views/order/index.vue')
  },
  {
    path: '/addresslist',
    name: 'addresslist',
    component: () => import('@/components/Addresslist.vue')
  },
  {
    path: '/addressadd',
    name: 'addressadd',
    component: () => import('@/components/AddressAdd.vue')
  },
  {
    path: '/register',
    name: 'register',
    components: {
      default: () => import('@/views/register/index.vue')
    }
  },
  {
    path: '/category',
    name: 'category',
    components: {
      default: () => import('@/views/category/index'),
      footer: Footer
    }
  },
  {
    path: '/product/:proid',
    name: 'product',
    component: () => import('@/views/product/index.vue')
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('@/views/search/index.vue')
  },
  {
    path: '/login',
    name: 'login',
    components: {
      default: () => import('@/views/login/index.vue')
    }
  },
  // 宣传页
  {
    path: '/leaflet',
    name: 'leaflet',
    components: {
      default: () => import('@/views/leaflet/index.vue')
    }
  },
  {
    path: '*',
    name: 'notfound',
    component: () => import('@/views/notfound/index')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.name === 'myuser' || to.name === 'cart') {
    if (!store.state.users.login_state) {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})
export default router
